cmake_minimum_required(VERSION 3.12)
project(MQTT-File-Transfer)

enable_testing()

enable_language(C)
set(CMAKE_C_STANDARD 99)
set(CMAKE_CXX_STANDARD 17)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

if(NOT CMAKE_BUILD_TYPE)
  set(CMAKE_BUILD_TYPE "Debug")
endif()

if(NOT CMAKE_SYSTEM_NAME)
  set(CMAKE_SYSTEM_NAME "Linux")
endif()
if(CMAKE_SYSTEM_NAME MATCHES "Linux")
  add_definitions(-DNEU_PLATFORM_LINUX)

elseif(CMAKE_SYSTEM_NAME MATCHES "Darwin")
  add_definitions(-DNEU_PLATFORM_DARWIN)

elseif(CMAKE_SYSTEM_NAME MATCHES "Windows")
  add_definitions(-DNEU_PLATFORM_WINDOWS)

endif()

if(NOT DISABLE_WERROR)
  set(CMAKE_C_FLAGS "$ENV{CFLAGS} -Werror")
endif()

if(USE_GCOV)
  message(STATUS "using gcov")
  SET(GCC_COVERAGE_COMPILE_FLAGS "-fprofile-arcs -ftest-coverage -O0")
  SET(GCC_COVERAGE_LINK_FLAGS "-lgcov")
  SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GCC_COVERAGE_COMPILE_FLAGS}")
  SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${GCC_COVERAGE_COMPILE_FLAGS}")
  SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_LINKER_FLAGS} ${GCC_COVERAGE_LINK_FLAGS}")
endif(USE_GCOV)

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -g")
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS} -O1")

add_definitions(-DDISABLE_ASAN)
if(NOT DISABLE_ASAN)
#  set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS} -fsanitize=address")
#  set(CMAKE_CXX_FLAGS_DEBUG "-Wall -g -fsanitize=address")
endif()

if (CMAKE_STAGING_PREFIX)
  include_directories(${CMAKE_STAGING_PREFIX}/include)
  include_directories(${CMAKE_SOURCE_DIR}/include)
  link_directories(${CMAKE_STAGING_PREFIX}/lib)
else()
  include_directories(/usr/local/include)
  include_directories(${CMAKE_SOURCE_DIR}/include)
  link_directories(/usr/local/lib)
endif()

find_package(Threads)

# 设置源文件
set(MQTT_SENDER_SOURCES
    # src/main.c
    src/sender_main.c
    src/file_sender.c
)

set(MQTT_RECEIVER_SOURCES
    # src/main.c
    src/receiver_main.c
    src/file_receiver.c
)

# 添加发送端可执行文件
add_executable(mqtt_sender ${MQTT_SENDER_SOURCES})

# 添加接收端可执行文件
add_executable(mqtt_receiver ${MQTT_RECEIVER_SOURCES})

# 设置目标属性
set_target_properties(mqtt_sender PROPERTIES
    RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin
)

set_target_properties(mqtt_receiver PROPERTIES
    RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin
)

# 基础库源文件
set(BASE_SOURCES
    connection/mqtt_client.c
    src/event/event_linux.c
    src/event/event_unix.c
    src/file_transfer_ctx.c
    utils/asprintf.c
    utils/json.c
    utils/base64.c
    utils/async_queue.c
    utils/utils.c
    ${PERSIST_SOURCES})

# 添加基础库
add_library(base SHARED ${BASE_SOURCES})
target_include_directories(base PUBLIC
    ${CMAKE_SOURCE_DIR}/include
)

# 链接库

target_link_libraries(base
    nng
    libzlog.so
    mbedtls mbedx509 mbedcrypto
    jansson
    ssl
    crypto
    ${CMAKE_THREAD_LIBS_INIT}
    m
    pthread
    dl
)

target_link_libraries(mqtt_sender
    base
)

target_link_libraries(mqtt_receiver
    base
)


# 设置安装目录
include(GNUInstallDirs)
if(UNIX)
        mark_as_advanced(CLEAR
            CMAKE_INSTALL_BINDIR
                CMAKE_INSTALL_INCLUDEDIR
                CMAKE_INSTALL_LIBDIR
                CMAKE_INSTALL_SYSCONFDIR)
else()
        set(CMAKE_INSTALL_BINDIR bin)
        set(CMAKE_INSTALL_INCLUDEDIR include)
        set(CMAKE_INSTALL_LIBDIR lib)
        set(CMAKE_INSTALL_SYSCONFDIR etc)
endif()

# 安装可执行文件
install(TARGETS mqtt_sender mqtt_receiver base
  RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
  LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
  ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})

# 安装头文件
install(DIRECTORY "${CMAKE_SOURCE_DIR}/include/"
        DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
        FILES_MATCHING
        PATTERN "*.h")

# 复制配置文件（如果需要）
if(EXISTS ${CMAKE_SOURCE_DIR}/config)
    file(GLOB CONFIG_FILES ${CMAKE_SOURCE_DIR}/config/*)
    file(COPY ${CONFIG_FILES} DESTINATION ${CMAKE_BINARY_DIR}/config)
endif()

# 打包配置
set(CPACK_PACKAGE_NAME "mqtt-file-transfer")
set(CPACK_PACKAGE_VERSION "1.0.0")
set(CPACK_PACKAGE_DESCRIPTION "MQTT File Transfer Demo")
set(CPACK_PACKAGE_CONTACT "Your Name <your.email@example.com>")
include(CPack)
